package com.example.shiwuyouproject.ui.fragment.my.activity

import android.view.LayoutInflater
import androidx.lifecycle.Observer
import com.example.shiwuyouproject.R
import com.example.shiwuyouproject.base.BaseVmActivity
import com.example.shiwuyouproject.databinding.ActivityMyapplicationBinding
import com.example.shiwuyouproject.ui.fragment.my.activity.adapter.MyApplicationAdapter
import com.example.shiwuyouproject.ui.fragment.my.activity.bean.MyApplicationBean
import com.example.shiwuyouproject.ui.fragment.my.activity.viewmodel.MyAppLicationViewModel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

/**
 * 我的申请列表
 */
class MyApplicationActivity:BaseVmActivity<ActivityMyapplicationBinding, MyAppLicationViewModel>() {
    override fun viewModelClass(): Class<MyAppLicationViewModel> = MyAppLicationViewModel::class.java

    override fun getViewBinding(): ActivityMyapplicationBinding {
        return ActivityMyapplicationBinding.inflate(layoutInflater)
    }

    override fun initView() {
        mBinding.myTitleBar.tvTitle.text = "我的申请"

    }
    private var myApplicationAdapter: MyApplicationAdapter?=null
    override fun initData() {
        myApplicationAdapter=MyApplicationAdapter()
        mBinding.myRecyclerView.adapter=myApplicationAdapter
        val mview = LayoutInflater.from(this).inflate(R.layout.view_nodata, null)
        myApplicationAdapter?.emptyView = mview
        mBinding.mySmartRefreshLayout.autoRefresh()
    }
    private var pageNumber=1
    override fun setListener() {
        mBinding.myTitleBar.tvBack.setOnClickListener { finish() }
      mBinding.mySmartRefreshLayout.setOnRefreshLoadMoreListener(object :
          OnRefreshLoadMoreListener {
          override fun onRefresh(refreshLayout: RefreshLayout) {
              pageNumber=1
              mViewModel.getApplyrLists(pageNumber.toString())
          }
          override fun onLoadMore(refreshLayout: RefreshLayout) {
              pageNumber++
              mViewModel.getApplyrLists(pageNumber.toString())
          }
      })
    }
    override fun observe() {
        mViewModel.getGenRenStatus.observe(this, Observer<MutableList<MyApplicationBean>>{
                if(pageNumber==1) {
                    myApplicationAdapter?.setNewData(it)
                    mBinding.mySmartRefreshLayout.finishRefresh()
                }  else{
                    myApplicationAdapter?.addData(it)
                    mBinding.mySmartRefreshLayout.finishLoadMore()
                }
             if(pageNumber>1&&it.size==0){
                 mBinding.mySmartRefreshLayout.finishLoadMoreWithNoMoreData()
             }
        })

    }
}